﻿using Abp.Dependency;
using Abp.Extensions;
using Abp.UI;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dysmsapi.Model.V20170525;
using Castle.Core.Logging;
using Magicodes.Admin.Identity;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Magicodes.Sms.Aliyun
{
    /// <summary>
    /// 阿里云短信验证码发送服务
    /// </summary>
    public class AliyunSmsSender : ISmsServiceSender, ISingletonDependency
    {
        private readonly IAliyunSmsSettting aliyunSmsSettting;
        private readonly AliyunSmsClient aliyunSmsClient;

        public ILogger Logger { get; set; } = NullLogger.Instance;
        public AliyunSmsSender(IAliyunSmsSettting aliyunSmsSettting)
        {
            this.aliyunSmsSettting = aliyunSmsSettting;
            this.aliyunSmsClient = new AliyunSmsClient(aliyunSmsSettting, Logger);
        }

        /// <summary>
        /// 发送短信验证码
        /// </summary>
        /// <param name="phone">手机号码</param>
        /// <param name="code">验证码</param>
        /// <returns></returns>
        public Task SendAsync(string phone, string code)
        {
            if (phone.IsNullOrWhiteSpace())
            {
                throw new UserFriendlyException("手机号码不能为空!");
            }
            if (code.IsNullOrWhiteSpace())
            {
                throw new UserFriendlyException("验证码不能为空!");
            }
            if (aliyunSmsSettting.SignName.IsNullOrWhiteSpace())
            {
                throw new UserFriendlyException("签名不能为空!");
            }
            var acsClient = aliyunSmsClient.AcsClient;
            SendSmsRequest request = new SendSmsRequest();
            try
            {
                //必填:待发送手机号。支持以逗号分隔的形式进行批量调用，批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
                request.PhoneNumbers = phone;
                //必填:短信签名-可在短信控制台中找到
                request.SignName = aliyunSmsSettting.SignName;
                //必填:短信模板-可在短信控制台中找到
                request.TemplateCode = aliyunSmsSettting.TemplateCode;
                //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
                request.TemplateParam = string.Format(aliyunSmsSettting.TemplateParam, code);
                //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
                request.OutId = aliyunSmsSettting.OutId;
                //请求失败这里会抛ClientException异常
                SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request);
                Logger.Info(sendSmsResponse.Message);
            }
            catch (ClientException e)
            {
                Logger.Error(e.ErrorMessage, e);
                throw new UserFriendlyException("短信发送失败!" + e.Message);
            }
            catch (Exception e)
            {
                Logger.Error(e.Message, e);
                throw new UserFriendlyException("短信发送失败!" + e.Message);
            }
            return Task.FromResult(0);
        }
    }
}
